package com.sgb.kjwl.view.ui.adapter.enterprise;


import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.enterprise.ListBean;
import com.sgb.kjwl.utils.PointUtils;
import com.swgk.core.util.UiUtil;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

/**
 * 作者:张磊
 * 日期:2018/6/20
 * 说明:需求item
 */
public class NeedAdapter extends BaseRecycleAdapter<ListBean> {

    private AskClick mAskClick;

    public AskClick getmAskClick() {
        return mAskClick;
    }

    public void setmAskClick(AskClick mAskClick) {
        this.mAskClick = mAskClick;
    }

    private boolean isHome;

    public NeedAdapter(Context context, List<ListBean> beans, boolean isHome) {
        super(context, R.layout.need_item, beans);
        this.isHome = isHome;
    }

    @Override
    public void convert(ItemViewHolder holder, ListBean item) {
        holder.setBinding(BR.viewModel, item);

        String KeyWord;
        String s = item.clicks + "";
        if (s.length() == 5) {
            KeyWord = getString(s, 1, 2, 3);
        } else if (s.length() == 6) {
            KeyWord = getString(s, 2, 3, 4);
        } else if (s.length() == 7) {
            KeyWord = getString(s, 3, 4, 5);
        } else if (s.length() == 8) {
            KeyWord = getString(s, 4, 5, 6);
        } else {
            KeyWord = item.clicks + "";
        }

        String numberStr = KeyWord + "人已参与";
        String needHeplStr = "需求说明：" + item.specifications;
        holder.setVisible(R.id.tv_tag, false)
                .setText(R.id.tv_requirementTypes, item.requirementTypes == 5 ? "服务" : "交易")
                .setText(R.id.tv_updateTime, UiUtil.getTime(item.createTime))
                .setText(R.id.tv_demandTitle, item.demandTitle)
                .setText(R.id.tv_linkMan, TextUtils.isEmpty(item.realName)? "": item.realName)
                .setText(R.id.tv_number, UiUtil.getKeyWordSpan(UiUtil.getColor(R.color.color_FFC000), numberStr, KeyWord))
                .setText(R.id.tv_need_help, UiUtil.getKeyWordSpan(UiUtil.getColor(R.color.color_666666), needHeplStr, "需求说明："));

        TextView textView = holder.getView(R.id.tv_budgetPrice);
        textView.setText(item.budgetPrice == -1 ? "议价" : TextViewBindingAdapter.setMoneyForRMB("¥" + PointUtils.price(item.budgetPrice)));

        if (!TextUtils.isEmpty(item.servicePrincipal) && item.requirementTypes > 4) {
            holder.getView(R.id.image_q).setVisibility(item.servicePrincipal.indexOf("1") > -1 ? View.VISIBLE : View.GONE);
            holder.getView(R.id.image_t).setVisibility(item.servicePrincipal.indexOf("2") > -1 ? View.VISIBLE : View.GONE);
//            Log.e("showlogo:"+ UiUtil.getTime(item.updateTime), holder.getView(R.id.image_q).getVisibility()+"___"+holder.getView(R.id.image_t).getVisibility());
        } else {
            holder.getView(R.id.image_q).setVisibility(View.GONE);
            holder.getView(R.id.image_t).setVisibility(View.GONE);
        }

        //临时调加区分是否大首页使用true 隐藏  false 显示
        if (isHome){
            holder.getView(R.id.tv_chat).setVisibility(View.GONE);
        }else {
            holder.getView(R.id.tv_chat).setVisibility(View.VISIBLE);
        }

        holder.getView(R.id.tv_chat).setOnClickListener(v -> {
            mAskClick.askClick(item);
        });
    }

    private String getString(String s, int i2, int i3, int i4) {
        String KeyWord;
        int pos1 = Integer.parseInt(s.substring(0, i2));
        int pos2 = Integer.parseInt(s.substring(i2, i3));
        int pos3 = Integer.parseInt(s.substring(i3, i4));
        if (pos3 >= 5) pos2++;
        if (pos2 == 10) {
            pos1++;
        }
        KeyWord = pos2 == 10 ? pos1 + ".0w" : pos1 + "." + pos2 + "w";
        return KeyWord;
    }


    public interface AskClick {
        void askClick(ListBean item);
    }
}
